<?php
$dbhost = 'localhost';  // mysql服务器主机地址
$dbuser = 'ether_gq';            // mysql用户名
$dbpass = '8MN6ZYmayNbRwxm7';          // mysql用户名密码
$dbname = 'ether_gq';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);


function add_friend($username,$friendname){
    global $conn;

    $a='SELECT * FROM user WHERE username='."'".$friendname."'";
    $sql=mysqli_query($conn,$a);

    $row = mysqli_fetch_array($sql);

    $friends_list=$row[3];
    $friends_list=json_decode($friends_list,true);

    $b=array_search ($username,$friends_list['friends']);

    if(!($b===false)){
        echo '2';
        exit;
    };

    array_push($friends_list['friends'],$username);
    $friends_list=json_encode($friends_list);

    $a='UPDATE user SET friends=\''.$friends_list.'\' WHERE username=\''.$friendname.'\'';

    if(mysqli_query($conn,$a)){
        echo '1';
    };
};

add_friend($_GET['username'],$_GET['friendname']);
add_friend($_GET['friendname'],$_GET['username']);

mysqli_close($conn);


?>